//
//  PingJiaDetailViewController.m
//  阿帕
//
//  Created 阿帕 on 2019/12/23.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "PingJiaDetailViewController.h"
#import "SGPagingView.h"

@interface PingJiaDetailViewController ()<SGPageTitleViewDelegate>

//@property (nonatomic, strong) GQRatingView *ratingView;
@property (nonatomic, strong) NSDictionary *myEvaluationDetailDic;
@property (nonatomic, strong) NSDictionary *evaluationMyDetailDic;
@property (nonatomic, strong) UIImageView *foundImageView;
@property (nonatomic, strong) UIView *myEvaluationBgView;
@property (nonatomic, strong) UIView *evaluationMyBgView;

@end

@implementation PingJiaDetailViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
        
    self.view.backgroundColor = BackgroundColor;
    
    if (_sourceType == 0) {
        NSArray *titleArr = @[@"我评价的", @"评价我的"];
        SGPageTitleViewConfigure *configure = [SGPageTitleViewConfigure pageTitleViewConfigure];
        configure.indicatorStyle = SGIndicatorStyleCover;
        configure.indicatorColor = Theme_ColorValue;
        configure.titleColor = Theme_ColorValue;
        configure.indicatorHeight = SY(30);
        configure.indicatorAdditionalWidth = 120;
        configure.titleSelectedColor = [UIColor whiteColor];
        /// pageTitleView
        SGPageTitleView *pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, 0, 270, 30) delegate:self titleNames:titleArr configure:configure];
        pageTitleView.layer.borderWidth = 1;
        pageTitleView.layer.borderColor = Theme_ColorValue.CGColor;
        pageTitleView.layer.cornerRadius = 3;
        pageTitleView.layer.masksToBounds = YES;
        pageTitleView.backgroundColor = [UIColor whiteColor];
        self.navigationItem.titleView = pageTitleView;
        
    } else {
        
        self.navigationItem.title = @"评价详情";
    }
    
    
    [self evaluationDetailRequest];
}

- (void)evaluationDetailRequest {
    WEAKSELF
            
    if (_sourceType == 0) {
        //从运单中心查看详情
       
        NSDictionary *dicData = @{
            @"orderDetailCode": _orderCode,
            @"commentType": _commentType,
            @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
        };
        
        [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], ORDER_COMMENT_ORDERCOMMENTBYDETAILCODE) withParameter:dicData withVC:self netName:@"运单评价详情" success:^(id response, NSString *msg) {
            
            if ([_commentType isEqual:@"1"]) {
                
                _myEvaluationDetailDic = response;
                
                if ([response allKeys].count <= 1) {
                    //数据为空
                    weakSelf.foundImageView.hidden = NO;
                } else {
                    weakSelf.foundImageView.hidden = YES;

                    [self setupMyEvaluationBaseView];
                }
                
            } else {
                
                _evaluationMyDetailDic = response;
                
                if ([response allKeys].count <= 1) {
                    //数据为空
                    weakSelf.foundImageView.hidden = NO;
                } else {
                    weakSelf.foundImageView.hidden = YES;

                    [self setupEvaluationMyBaseView];
                }
            }
            
            
        } failure:^(NSInteger code, NSString *msg) {
            
        }];
        
        
        
    } else {
        //从个人中心我的评价查看详情
        
        NSDictionary *dicData = @{
            @"code": _orderCode,
            @"commentType": _commentType,
            @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
        };
        
        [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], ORDERCOMMENT_ORDERCOMMENTBYCODE) withParameter:dicData withVC:self netName:@"评价列表详情" success:^(id response, NSString *msg) {
            
            if ([_commentType isEqual:@"1"]) {
                
                _myEvaluationDetailDic = response;
                
                if ([response allKeys].count <= 1) {
                    //数据为空
                    weakSelf.foundImageView.hidden = NO;
                } else {
                    weakSelf.foundImageView.hidden = YES;

                    [self setupMyEvaluationBaseView];
                }
                
            } else {
                
                _evaluationMyDetailDic = response;
                
                if ([response allKeys].count <= 1) {
                    //数据为空
                    weakSelf.foundImageView.hidden = NO;
                } else {
                    weakSelf.foundImageView.hidden = YES;

                    [self setupEvaluationMyBaseView];
                }
            }
                        
        } failure:^(NSInteger code, NSString *msg) {
            
        }];
        
    }
    
    
}

//按钮点击事件
- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex {
    
    switch (selectedIndex) {
        case 0:
            _commentType = @"1";
            _myEvaluationBgView.hidden = NO;
            _evaluationMyBgView.hidden = YES;
            
            break;
        case 1:
            _commentType = @"0";
            _myEvaluationBgView.hidden = YES;
            _evaluationMyBgView.hidden = NO;
            
            break;
            
        default:
            break;
    }
    
    [self evaluationDetailRequest];
}

///我评价的
- (void)setupMyEvaluationBaseView {

    if (_myEvaluationBgView) {
        [_myEvaluationBgView removeAllSubviews];
    }
    
    NSArray *titles = @[@"货物相符性", @"付款时效性", @"地址准确性", @"装卸时效性", @"综合满意度"];
    for (int i=0; i<titles.count; i++) {
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10 + 30*i, 80, 30)];
        textLabel.font = font(16);
        textLabel.text = titles[i];
        textLabel.adjustsFontSizeToFitWidth = YES;
        textLabel.textColor = Dark_TextColor;
        [self.myEvaluationBgView addSubview:textLabel];
    }
    
    //五角星 链式调用
    GQRatingView *hyRatingView = [GQRatingView init]
    .frameChain(CGPointMake(100, 15), 20)
    .canTouchChain(NO)
    .needIntValueChain(YES)
    .scoreNumChain([NSNumber numberWithFloat:[_myEvaluationDetailDic[@"truthGrade"] floatValue]])
    .normalColorChain([UIColor cy_colorWithHexString:@"#C0CDDE"])
    .highlightColorChian([UIColor cy_colorWithHexString:@"#FFC000"])
    .scroreBlockChain(^(NSNumber *scoreNumber){
        NSLog(@"%@",scoreNumber);
    }).superViewChain(self.myEvaluationBgView);
    
    GQRatingView *fkRatingView = [GQRatingView init]
    .frameChain(CGPointMake(100, 45), 20)
    .canTouchChain(NO)
    .needIntValueChain(YES)
    .scoreNumChain([NSNumber numberWithFloat:[_myEvaluationDetailDic[@"timeGrade"] floatValue]])
    .normalColorChain([UIColor cy_colorWithHexString:@"#C0CDDE"])
    .highlightColorChian([UIColor cy_colorWithHexString:@"#FFC000"])
    .scroreBlockChain(^(NSNumber *scoreNumber){
        NSLog(@"%@",scoreNumber);
    }).superViewChain(self.myEvaluationBgView);
    
    GQRatingView *addressGradeRatingView = [GQRatingView init]
    .frameChain(CGPointMake(100, 75), 20)
    .canTouchChain(NO)
    .needIntValueChain(YES)
    .scoreNumChain([NSNumber numberWithFloat:[_myEvaluationDetailDic[@"addressGrade"] floatValue]])
    .normalColorChain([UIColor cy_colorWithHexString:@"#C0CDDE"])
    .highlightColorChian([UIColor cy_colorWithHexString:@"#FFC000"])
    .scroreBlockChain(^(NSNumber *scoreNumber){
    }).superViewChain(self.myEvaluationBgView);
    
    GQRatingView *loadGradeRatingView = [GQRatingView init]
    .frameChain(CGPointMake(100, 105), 20)
    .canTouchChain(NO)
    .needIntValueChain(YES)
    .scoreNumChain([NSNumber numberWithFloat:[_myEvaluationDetailDic[@"loadGrade"] floatValue]])
    .normalColorChain([UIColor cy_colorWithHexString:@"#C0CDDE"])
    .highlightColorChian([UIColor cy_colorWithHexString:@"#FFC000"])
    .scroreBlockChain(^(NSNumber *scoreNumber){
        
    }).superViewChain(self.myEvaluationBgView);
    
    GQRatingView *multipleGradeRatingView = [GQRatingView init]
    .frameChain(CGPointMake(100, 135), 20)
    .canTouchChain(NO)
    .needIntValueChain(YES)
    .scoreNumChain([NSNumber numberWithFloat:[_myEvaluationDetailDic[@"multipleGrade"] floatValue]])
    .normalColorChain([UIColor cy_colorWithHexString:@"#C0CDDE"])
    .highlightColorChian([UIColor cy_colorWithHexString:@"#FFC000"])
    .scroreBlockChain(^(NSNumber *scoreNumber){
        
    }).superViewChain(self.myEvaluationBgView);
    
    UIView *hLine = [[UIView alloc] initWithFrame:CGRectMake(15, 175, ScreenW-30, 1)];
    hLine.backgroundColor = BackgroundColor;
    [self.myEvaluationBgView addSubview:hLine];
    
    UILabel *pingjiaContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, hLine.cy_bottom, ScreenW-30, 40)];
    pingjiaContentLabel.font = font(12);
    pingjiaContentLabel.numberOfLines = 0;
    pingjiaContentLabel.text = _myEvaluationDetailDic[@"commentContent"];
    pingjiaContentLabel.adjustsFontSizeToFitWidth = YES;
    pingjiaContentLabel.textColor = Dark_TextColor;
    [self.myEvaluationBgView addSubview:pingjiaContentLabel];
    
    NSArray *imageArray = _myEvaluationDetailDic[@"imageUrlList"];
    
//    NSArray *imageArray = @[@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",];
    
    
    float width = (ScreenW-75)/4;
    
    for (int i=0; i<imageArray.count; i++) {
        
        int x = i%4;
        int y = i/4;
        
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x*width+(15*(x+1)), y*width + (10*(y+1)) + pingjiaContentLabel.cy_bottom, width, width)];
        setNetImage(image, imageArray[i], @"gcy_icon_plaholder");
        image.userInteractionEnabled = YES;
        [self.myEvaluationBgView addSubview:image];
        
        [image nyl_addTapGes:^{
            HZPhotoBrowser *browser = [[HZPhotoBrowser alloc] init];
            browser.isFullWidthForLandScape = YES;
            browser.isNeedLandscape = YES;
            browser.currentImageIndex = i;
            browser.imageArray = imageArray;
            [browser show];
        }];
    }
}

///评价我的
- (void)setupEvaluationMyBaseView {
 
    if (_evaluationMyBgView) {
        [_evaluationMyBgView removeAllSubviews];
    }
    
    
    NSArray *titleArr = @[@"配送速度",@"服务态度",@"运输安全",@"货物损耗度",@"综合满意度"];
    NSArray *valueArr = @[
        CJStringWithFormat(@"%@",_evaluationMyDetailDic[@"speedGrade"]),
        CJStringWithFormat(@"%@",_evaluationMyDetailDic[@"serviceGrade"]),
        CJStringWithFormat(@"%@",_evaluationMyDetailDic[@"safetyGrade"]),
        CJStringWithFormat(@"%@",_evaluationMyDetailDic[@"lossGrade"]),
        CJStringWithFormat(@"%@",_evaluationMyDetailDic[@"satisfactionGrade"]),
    ];
    
    for (int i=0; i<titleArr.count; i++) {
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 10 + 30*i, ScreenW, 30)];
        bgView.backgroundColor = [UIColor whiteColor];
        [self.evaluationMyBgView addSubview:bgView];
        
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 80, bgView.cy_height)];
        textLabel.font = font(16);
        textLabel.text = titleArr[i];
        textLabel.adjustsFontSizeToFitWidth = YES;
        textLabel.textColor = Dark_TextColor;
        [bgView addSubview:textLabel];
        
        [GQRatingView init]
        .frameChain(CGPointMake(textLabel.cy_right + 5, textLabel.cy_origin.y + 5),20)
        .canTouchChain(NO)
        .needIntValueChain(YES)
        .scoreNumChain([NSNumber numberWithFloat:[valueArr[i] floatValue]])
        .normalColorChain([UIColor cy_colorWithHexString:@"#b5caea"])
        .highlightColorChian([UIColor cy_colorWithHexString:@"#ff7e15"])
        .scroreBlockChain(^(NSNumber *scoreNumber){
        }).superViewChain(bgView);
    }
    
    UIView *hLine = [[UIView alloc] initWithFrame:CGRectMake(15, 175, ScreenW-30, 1)];
    hLine.backgroundColor = BackgroundColor;
    [self.evaluationMyBgView addSubview:hLine];
    
    UILabel *pingjiaContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, hLine.cy_bottom, ScreenW-30, 40)];
    pingjiaContentLabel.font = font(12);
    pingjiaContentLabel.numberOfLines = 0;
    pingjiaContentLabel.text = _evaluationMyDetailDic[@"commentContent"];
    pingjiaContentLabel.adjustsFontSizeToFitWidth = YES;
    pingjiaContentLabel.textColor = Dark_TextColor;
    [self.evaluationMyBgView addSubview:pingjiaContentLabel];
    
    //    NSArray *imageArray = @[@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",];
    
    NSArray *imageArray = _evaluationMyDetailDic[@"imageUrlList"];
    
    float width = (ScreenW-75)/4;
    
    for (int i=0; i<imageArray.count; i++) {
        
        int x = i%4;
        int y = i/4;
        
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x*width+(15*(x+1)), y*width + (10*(y+1)) + pingjiaContentLabel.cy_bottom, width, width)];
        setNetImage(image, imageArray[i], @"gcy_icon_plaholder");
        image.userInteractionEnabled = YES;
        [self.evaluationMyBgView addSubview:image];
        
        [image nyl_addTapGes:^{
            HZPhotoBrowser *browser = [[HZPhotoBrowser alloc] init];
            browser.isFullWidthForLandScape = YES;
            browser.isNeedLandscape = YES;
            browser.currentImageIndex = i;
            browser.imageArray = imageArray;
            [browser show];
        }];
    }
}

- (UIImageView *)foundImageView {
    if (!_foundImageView) {
        _foundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 170, 170)];
        _foundImageView.image = image(@"gcy_img_list_found");
        _foundImageView.contentMode = UIViewContentModeScaleAspectFit;
        _foundImageView.center = self.view.center;
        _foundImageView.cy_centerY -= 70;
        [self.view addSubview:_foundImageView];
    }
    return _foundImageView;
}

- (UIView *)myEvaluationBgView {
    if (!_myEvaluationBgView) {
        _myEvaluationBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, ScreenW, ScreenH-NavHeight-10)];
        _myEvaluationBgView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_myEvaluationBgView];
    }
    return _myEvaluationBgView;
}

- (UIView *)evaluationMyBgView {
    if (!_evaluationMyBgView) {
        _evaluationMyBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, ScreenW, ScreenH-NavHeight-10)];
        _evaluationMyBgView.backgroundColor = [UIColor whiteColor];
//        _evaluationMyBgView.hidden = YES;

        [self.view addSubview:_evaluationMyBgView];
    }
    return _evaluationMyBgView;
}
@end
